﻿using LuaInterface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xframe_server.Utils
{
    class ProtoFormator
    {
        public static byte[] Format(int id, LuaTable table)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            foreach (DictionaryEntry item in table)
            {
                dic[item.Key.ToString()] = item.Value;
            }
            if(dic["protoID"] == null)
            {
                dic["protoID"] = id.ToString();
            }
            string json = JsonConvert.SerializeObject(dic);
            return json.GetBytes();
        }
        public static Dictionary<object, object> DeFormat(byte[] bytes)
        {
            Dictionary<object, object> dic = new Dictionary<object, object>();
            var json = bytes.EncodeToString();
            dic = JsonConvert.DeserializeObject<Dictionary<object, object>>(json);
            return dic;
        }
    }
}
